// mircoApp/assessment/assessment_questionnaire/assessment_questionnaire.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: "",
    show: false,
    isfree: false,
    imgHost: "",
    isView: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      options: options,
      imgHost: `${app.globalData.imgPath}/assessment/assessment_icon_1.png`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  init() {
    this.setData({
      questionInd: 0,
      info: "",
      question_list: {},
      show: false,
    })
  },
  async submitM() {
    for (let i = 0; i < this.data.has_many_question.length; i++) {
      if (!this.data.question_list.hasOwnProperty(this.data.has_many_question[i].id)) {
        app.tips(`选项${i + 1}请完善`)
        return;
      }
    }
    await this.submitPost();
  },
  submitPost() {
    let json = {
      id: this.data.info.id,
      question_list: this.data.question_list
    };
    app._postNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.submit"),
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.resultPost(res.data.id);
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  resultPost(rid) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.resultDetail"),
      data: {
        id: rid
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            show: true,
            resultDetail: res.data
          })
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  watchReport() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.resultDetail"),
      data: {
        id: this.options.id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          if (res.data.is_pay == 1) {
            wx.navigateTo({
              url: `/mircoApp/assessment/assessmentReport/assessmentReport?id=${this.data.resultDetail.id}`,
            })
          }
          this.setData({
            show: true,
            resultDetail: res.data
          })
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  Unlock() {
    if (this.data.resultDetail.is_pay == 0 && this.data.resultDetail.rest_count > 0) {
      // 免费
      app._getNetWork({
        url: app.getNetAddresss("plugin.assessment.frontend.question.freeResult"),
        data: {
          id: this.data.resultDetail.id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            wx.navigateTo({
              url: `/mircoApp/assessment/assessmentReport/assessmentReport?id=${this.data.resultDetail.id}`,
            })
          } else {
            app.tips(res.msg);
          }
        }
      });
    } else {
      // 付费
      app._getNetWork({
        url: app.getNetAddresss("plugin.assessment.frontend.create.index"),
        data: {
          ass_log_id: this.data.resultDetail.id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            wx.navigateTo({
              url: `/packageD/buy/orderPay/orderPay?order_id=${res.data.order_ids}&ass_log_id=${this.data.resultDetail.id}&plugName=orderpay_assessment`,
            })
          } else {
            app.tips(res.msg);
          }
        }
      });
    }
  },
  prevShow() {
    this.setData({
      questionInd: this.data.questionInd - 1
    })
  },
  showNext() {
    this.setData({
      questionInd: this.data.questionInd + 1
    })
  },
  cancelTap() {
    this.setData({
      show: false
    })
  },
  radioChange(e) {
    let {
      id,
      ind
    } = e.currentTarget.dataset;
    this.data.question_list[ind] = id;
    this.setData({
      question_list: this.data.question_list
    })
  },
  getData() {
    let json = {}
    if (!this.options.view) {
      json.third_cat_id = this.options.id;
    } else {
      json.id = this.options.id;
      this.getDateil(json);
      return
    }
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.detail"),
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          this.setData({
            info: res.data,
            has_many_question: res.data.has_many_question
          })
        } else {
          wx.navigateBack({
            delta: 1
          })
          app.tips(res.msg);
        }
      }
    });
  },
  getDateil(json) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.questionDetail"),
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          this.setData({
            has_many_question: res.data.has_many_question
          })
          wx.setNavigationBarTitle({
            title: res.data.name,
          })
        } else {
          app.tips(res.msg);
        }
      }
    });
  }
})